import MySQL
import People
import Events
import PeopleOnEvents
import TripExpenses
import SubGroupExpenses
import PeopleInSubGroup
import TripExpenseAcceptStatus


class Database:
    def __init__(self):
        self.SQL = MySQL.Connection()
        self.People = People.People(self.SQL)
        self.Events = Events.Events(self.SQL)
        self.PeopleOnEvents = PeopleOnEvents.PeopleEvents(self.SQL)
        self.Expenses = TripExpenses.Expenses(self.SQL)
        self.SubGroupExpenses = SubGroupExpenses.SubGroupExpenses(self.SQL)
        self.PeopleInSubGroups = PeopleInSubGroup.PeopleInSubGroup(self.SQL)
        self.TripExpenseStatus = TripExpenseAcceptStatus.TripExpenseAcceptSatus(self.SQL)
        
    def RemovePersonFromEvent(self, PID, EID):
        self.PeopleOnEvents.RemovePersonFromEvent(EID, PID)
        Expenses = self.Expenses.GetExpenses(EID = EID, PID = PID)
        for Expense in Expenses:
            self.Expenses.RemoveExpense(Expense.TexID)
        SGExpenses = self.SubGroupExpenses.GetSubGroupExpenses(EID = EID, PID= PID)
        for Expense in SGExpenses:
            self.RemoveSubGroupExpense(Expense.SgExp_id)
        self.PeopleInSubGroups.RemovePersonFromEvent(EID, PID)
        
            
    def GetEventsPersonOn(self, PID):
        EventObjs = []
        EventEIDs = self.PeopleOnEvents.GetAllEventsPersonOn(PID)
        EventDict =  self.Events.GetAllEventsDict()
        for EID in EventEIDs:
            EventObjs.append(EventDict[EID])
        return EventObjs
    
    def GetPeopleOnEvent(self, EID):
        PeopleObjs = []
        PeoplePIDs = self.PeopleOnEvents.GetAllPeopleOnEvent(EID)
        PeopleDict = self.People.GetAllPeopleDict()
        for PID, Prop in PeoplePIDs:
            PeopleObjs.append(PeopleDict[PID])
        return PeopleObjs
     
    def GetExpensesOfPerson(self, EID, PID):
        ##returns[[Exp,Prop],[Exp,Prop]]
        TotalManDays = self.PeopleOnEvents.GetTotalEventManDays(EID)
        PIDManDays = self.PeopleOnEvents.GetPersonsManDays(EID = EID, PID = PID)
        Prop = (1.0*PIDManDays)/(TotalManDays*1.0)
        Expenses = self.Expenses.GetExpenses(EID=EID)
        Output = []
        for Expense in Expenses:
            Output.append([Expense, Prop])
        return Output
    
    def GetSubgroupExpensesOfPerson(self, PID, EID):
        ##return [[SubGroupExpense, Prop],[[SubGroupExpense, Prop]]]
        SubGroupsExpenseIDs = self.PeopleInSubGroups.GetAllSubGroupsPersonInOnEvent(PID, EID)
        SubGroupExpenses = self.SubGroupExpenses.GetAllSubGroupExpensesDict()
        Output = []
        for ExpenseID, Prop in SubGroupsExpenseIDs:
            Output.append([SubGroupExpenses[ExpenseID], Prop])
        return Output
    
    def UpdateSubGroupExpense(self, Expense, PIDs):
        self.SubGroupExpenses.UpdateSubGroupExpense(Expense)
        self.PeopleInSubGroups.RemoveSubGroup(Expense.SgExp_id)
        for PID, Prop in PIDs:
            self.PeopleInSubGroups.AddPersonToSubGroup(PID, Expense.EID, Expense.SgExp_id, Prop)
        
        
    def AddSubGroupExpense(self, EID, PID, Desc, Amount, Date, AddedPIDs):
        self.SubGroupExpenses.AddSubGroupExpense(EID, PID, Date, Desc, Amount)
        Expense = self.SubGroupExpenses.GetSubGroupExpenses(EID=EID, PID=PID, Exp_Discription = Desc, Exp_Date = Date)[0]
        for PID, Prop in AddedPIDs:
            self.PeopleInSubGroups.AddPersonToSubGroup(PID, Expense.EID, Expense.SgExp_id, Prop)
        
    def RemoveSubGroupExpense(self, SgExp_id):
        self.SubGroupExpenses.RemoveSubGroupExpense(SgExp_id)
        self.PeopleInSubGroups.RemoveSubGroup(SgExp_id)
        
    


